import { Result } from '@/utils/types'
import api from '@/libs/request'
import { encryptAccept } from '@/utils/rsa'
import { GrantType, GrantTypes, SignResp } from '@/apis/sign/type'
import { CaptchaRequest, CaptchaResponse } from '@/components/slide-captcha/Captcha'
import storage from '@/utils/storage'

export const getLoginPermShops = (username: string) => {
    return api.get(`/gruul-mall-uaa/uaa/auth/shop/${username}`)
}

/**
 * 获取滑块验证码
 */
export const doGetCaptchaSlider = () => {
    return api.get<CaptchaResponse>(`gruul-mall-uaa/uaa/auth/captcha/slider`)
}

/**
 * 获取验证码
 */
export const doPostSmsCode = (data: CaptchaRequest<{ mobile: string } | string>) => {
    let mobile = ''
    if (typeof data.form === 'string') {
        mobile = data.form
    } else {
        mobile = data.form.mobile
    }
    encryptAccept((encrypt) => {
        mobile = encrypt(mobile)
    })
    data = { ...data, form: { mobile } }
    return api.post(`gruul-mall-uaa/uaa/auth/captcha/sms`, data)
}

/**
 * login
 * @param {GrantType} grantType 认证方式类型
 * @param data 认证资料
 */
export const signByUser = <T extends GrantType = GrantType>(grantType: T, data: any): Promise<Result<SignResp>> => {
    const grant = GrantTypes[grantType]
    //参数加密
    if (grant.encrypt) {
        encryptAccept((encrypt) => {
            grant.encryptFields?.forEach((field) => {
                data[field] = encrypt(data[field])
            })
        })
    }
    return api.post<SignResp>(`/gruul-mall-uaa/login?grant_type=${grant.value}`, data)
}

export const myData = () => {
    return api.get('gruul-mall-uaa/uaa/shop/admin/mine')
}
/**
 * 商家中心 查询账户信息
 */
export const doGetUserDataAccount = () => {
    return api.get(`gruul-mall-uaa/uaa/user/data/account`)
}
/**
 * 商家中心 发送重置密码短信验证码
 */
export const doGetMyResetPasswordSms = () => {
    return api.get(`gruul-mall-uaa/uaa/auth/reset/my/password/sms`)
}
/**
 * 商家中心 重置密码
 * @param {string} code
 * @param {string} password
 * @param {string} confirmPassword
 */
export const doPutMyResetPassword = (code: string, password: string, confirmPassword: string) => {
    return api.put(`gruul-mall-uaa/uaa/auth/reset/my/password`, {
        code,
        password,
        confirmPassword,
    })
}
/**
 * 重置密码 发送重置密码短信验证码
 */
export const doGetResetPasswordSms = (mobile: string) => {
    return api.get(`gruul-mall-uaa/uaa/auth/reset/${mobile}/password/sms`)
}
/**
 * 重置密码
 * @param {string} code
 * @param {string} password
 * @param {string} confirmPassword
 * @param {string} mobile
 */
export const doPutResetPassword = (code: string, password: string, confirmPassword: string, mobile: string) => {
    return api.put(`gruul-mall-uaa/uaa/auth/reset/password`, {
        code,
        password,
        confirmPassword,
        mobile,
    })
}

/**
 * 获取可切换的店铺列表
 * @returns
 */
export const doGetEnableChangeShop = () => {
    return api.get('gruul-mall-uaa/uaa/auth/switch')
}

/**
 * 退出登录
 */
export const doPostLogout = () => {
    storage.removeThis()
    return api.post('gruul-mall-uaa/logout')
}
